
package com.xbongbong.admin.model.impl;

import com.xbongbong.admin.domain.dao.RaidersScoreExchangeDao;
import com.xbongbong.admin.domain.entity.RaidersScoreExchangeEntity;
import com.xbongbong.admin.model.RaidersScoreExchangeModel;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.RaidersErrorCodeEnum;
import com.xbongbong.pro.enums.raiders.enums.RaidersVerificationEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
* @author autoGen
* @time 2019-09-18 13:56:16
*/
@Service("raidersScoreExchangeModel")
public class RaidersScoreExchangeModelImpl implements RaidersScoreExchangeModel {

	@Resource
	private RaidersScoreExchangeDao dao;
	@Override
	public Integer insert(RaidersScoreExchangeEntity entity){
		long now = DateUtil.getInt();
		entity.setAddTime(now);
		entity.setUpdateTime(now);

		return dao.insert(entity);
	}
	@Override
	public Integer update(RaidersScoreExchangeEntity entity){
		long now = DateUtil.getInt();
		entity.setUpdateTime(now);

		return dao.update(entity);
	}

	@Override
	public Integer save(RaidersScoreExchangeEntity entity){

		if(entity.getId() == null || Objects.equals(0L, entity.getId())) {
			return insert(entity);
		}
		return update(entity);
	}

	@Override
	public void insertBatch(List<RaidersScoreExchangeEntity> entityList) {
    	dao.insertBatch(entityList);
    }

	@Override
	public Integer deleteByKey( Long key,  String corpid){
		return dao.deleteByKey(key , corpid);
	}

	@Override
	public RaidersScoreExchangeEntity getByKey( Long key,  String corpid){
		return dao.getByKey(key , corpid);
	}

	@Override
	public List<RaidersScoreExchangeEntity> findEntitys(Map<String ,Object>  param){
		return dao.findEntitys(param);
	}

    @Override
	public Integer getEntitysCount(Map<String ,Object>  param){
		return dao.getEntitysCount(param);
	}

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
	public RaidersScoreExchangeEntity getByRedeemCode(String redeemCode) throws XbbException {
		if (!StringUtil.isEmpty(redeemCode)) {
			//去除空格
			redeemCode = StringUtil.trim(redeemCode);
		}
		RaidersScoreExchangeEntity exchange = dao.getByRedeemCode(redeemCode);
		if (exchange == null || Objects.equals(exchange.getDel(), DelEnum.DELETE.getDel())) {
			throw new XbbException(RaidersErrorCodeEnum.API_ERROR_800005, String.format(RaidersErrorCodeEnum.API_ERROR_800005.getMsg(), "兑换记录"));
		}
		if (Objects.equals(exchange.getVerification(), RaidersVerificationEnum.VERIFICATION.getCode())) {
			throw new XbbException(RaidersErrorCodeEnum.API_ERROR_800020, String.format(RaidersErrorCodeEnum.API_ERROR_800020.getMsg(), "兑换记录", "核销", "核销"));
		}
		return exchange;
	}


}

